﻿using DreamCMS.BLL.Factory;
using DreamCMS.BLL.Factory.Interface;
using DreamCMS.Entity;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DreamCMS.BLL
{
    public class UserManager
    {
        #region Init
        private static IUserDAO dao = DataAccessFactory.CreateUserDAO();
        private static List<UserInfo> userList = null;
        private static object lockHelper = new object();
        static UserManager()
        {
            InitUserList();
        }
        /// <summary>
        /// 初始化用户列表
        /// </summary>
        private static void InitUserList()
        {
            if (userList == null)
            {
                lock (lockHelper)
                {
                    if (userList == null)
                    {
                        userList = dao.GetAllUsers();//拿到后，已经按照GetAllUser这个方法里使用的sql语句进行了排序，userList不需要再次排序了。因为userList的排序比较器与sql语句一致。
                    }
                }
            }
        }
        /// <summary>
        /// 更新用户信息管理器的缓存，重新从数据库获取User用户
        /// </summary>
        public static void ClearCache()
        {
            userList = dao.GetAllUsers();
        }
        #endregion

        #region Get
        /// <summary>
        /// 根据ID获取用户实体
        /// </summary>
        /// <param name="userId"></param>
        /// <returns></returns>
        public static UserInfo GetOneUser(long userId)
        {
            if(UserManager.userList==null)
            {
                UserManager.InitUserList();
            }
            foreach (UserInfo u in userList)
            {
                if (u.Userid == userId)
                {
                    return u;
                }
            }
            return null;
        }
        /// <summary>
        /// 获取所有用户集合
        /// </summary>
        /// <returns></returns>
        public static List<UserInfo> GetUsers()
        {
            if (UserManager.userList == null)
            {
                UserManager.InitUserList();
            }
            return userList;
        }
        /// <summary>
        /// 根据用户名和密码获取用户（用在用户登录时的校验）
        /// </summary>
        /// <param name="userName"></param>
        /// <param name="password"></param>
        /// <returns></returns>
        public static UserInfo GetUser(string userName, string password)
        {
            if (UserManager.userList == null)
            {
                UserManager.InitUserList();
            }
            foreach (UserInfo user in userList)
            {
                if (user.UserName == userName && user.Password == password)
                {
                    return user;
                }
            }
            return null;
        }
        public static UserInfo GetUserByUserName(string userName)
        {
            if (UserManager.userList == null)
            {
                UserManager.InitUserList();
            }
            foreach (UserInfo u in userList)
            {
                if (u.UserName == userName)
                {
                    return u;
                }
            }
            return null;
        }

#endregion

        #region Update
        /// <summary>
        /// 更新用户信息
        /// </summary>
        /// <param name="user">用户新实体</param>
        /// <returns></returns>
        public static bool UpdateUser(UserInfo user)
        {
            if (dao.UpdateUser(user) > 0)
            {
                userList.Sort();
                return true;
            }
            return false;
        }
        /// <summary>
        /// 更新用户发表的评论数
        /// </summary>
        /// <param name="userId">用户ID</param>
        /// <param name="addCount">增加的量</param>
        /// <returns>是否更新成功</returns>
        public static bool UpdateUserCommentCount(long userId, int addCount)
        {
            UserInfo user = GetOneUser(userId);
            if (user != null && dao.UpdateUser(user) > 0) //更新数据库
            {
                user.Commentcount += addCount; //更新缓存
                return UpdateUser(user);
            }
            return false;
        }
        /// <summary>
        /// 更新用户文章数量的统计
        /// </summary>
        /// <param name="userId"></param>
        /// <param name="addCount"></param>
        /// <returns></returns>
        public static bool UpdateUserPostCount(long userId, long addCount)
        {
            UserInfo user = GetOneUser(userId);
            if (dao.UpdateUser(user) > 0 && user != null)  //更新数据库中该用户文章数
            {
                user.Postcount += addCount; //更新缓存中的该用户文章数
                return true;
            }
            return false;
        }
        #endregion

        #region Insert
        /// <summary>
        /// 添加用户
        /// </summary>
        /// <param name="user">用户实体</param>
        /// <returns>返回新用户ID</returns>
        public static long InsertUser(UserInfo user)
        {
            long rel = dao.AddUser(user);
            if (rel > 0)
            {
                user.Userid = rel; //注意：一定要把最新的ID给缓存里的信息实体。
                userList.Add(user); //同步缓存
                userList.Sort(); //新添加了用户排序
                return rel;  //返回新添加用户的ID
            }
            return 0;  //0表示没有添加成功
        }
        #endregion

        #region Delete
        /// <summary>
        /// 删除用户
        /// </summary>
        /// <param name="userId">用户ID</param>
        /// <returns>是否删除成功</returns>
        public static bool DeleteUser(long userId)
        {
            if (dao.DeleteUser(userId) > 0)
            {
                userList.Remove(GetOneUser(userId)); //同步删除缓存
                return true;
            }
            return false;
        }
        #endregion

        #region Others
        /// <summary>
        /// 用户名是否存在
        /// </summary>
        /// <param name="userName">用户名</param>
        /// <returns>该用户名是否存在</returns>
        public static bool IsUserExist(string userName)
        {
            return dao.IsUserExist(userName);
        }
        #endregion

    }
}
